package org.raymond.icloud.modules.order.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.raymond.icloud.common.core.util.RedisUtil;
import org.raymond.icloud.modules.order.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

/**
 * @author raymond
 * @version V1.0
 * @Description: TODO
 */
@Slf4j
@Service
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private RedisUtil redisUtil;
    private final static String INV_KEY = "inv:remain";

    @Override
    public void batchReduceStock() {
        Long remain = redisUtil.decr(INV_KEY, 1);
        log.info("{}: {}", INV_KEY, remain);
        if(remain<=0){
            log.info("sold out");
        }else{
            log.info("create order for remain:{}", remain);
        }
    }

    @PostConstruct
    public void init(){
        redisUtil.set(INV_KEY, 100);
    }
}
